package com.java.zhuiyun.api.course_pc.service.impl;

import com.java.zhuiyun.api.course_pc.model.BackCourseModel;
import com.java.zhuiyun.api.course_pc.service.CoursePcService;
import com.java.zhuiyun.api.course_pc.entity.CourseUnit;
import com.java.zhuiyun.api.course_pc.entity.FrontDeskUser;
import com.java.zhuiyun.api.course_pc.mapper.CoursePcMapper;
import com.java.zhuiyun.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName CoursePcServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/8/21 11:48
 **/
@Service
public class CoursePcServiceImpl implements CoursePcService {
    @Autowired
    private CoursePcMapper coursePcMapper;
    /*
    * @Author wuwan
    * @Description todo 查询老师信息
    * @Date 2023/8/22 14:20
    * @Return
    * @param null
    */
    @Override
    public CommonResult getTeacher(Integer id) {
        return CommonResult.ok(coursePcMapper.getTeacher(id));
    }

    /*
    * @Author wuwan
    * @Description todo 查询课程
    * @Date 2023/8/21 15:02
    * @Return
    * @param null
    */
    @Override
    public CommonResult selectCourse(Integer tid) {
        List<BackCourseModel> courses = coursePcMapper.selectCourse(tid);
        for (int i = 0; i < courses.size(); i++) {
            BackCourseModel courseModel = courses.get(i);
            Integer courseClick = courseModel.getCourseClick();
            Integer courseClicks = courseClick+1;
            System.out.println(courseClicks);
            coursePcMapper.upCourseClick(tid,courseClicks);
        }

        return CommonResult.ok(courses);
    }
    /*
    * @Author wuwan
    * @Description todo 查询章节
    * @Date 2023/8/21 15:24
    * @Return
    * @param null
    */
    @Override
    public CommonResult selectUnit() {
        return CommonResult.ok(coursePcMapper.selectUnit());
    }
    /*
    * @Author wuwan
    * @Description todo 视频信息查询
    * @Date 2023/8/22 14:19
    * @Return
    * @param null
    */
    @Override
    public CommonResult playVideo(Integer fid) {
        CourseUnit courseUnit = coursePcMapper.playVideo(fid);
        System.out.println(courseUnit);
        return CommonResult.ok(courseUnit);
    }
    /*
    * @Author wuwan
    * @Description 用户课程购买
    * @Date 2023/8/23 19:09
    * @Return
    * @param null
    */
    @Override
    public String buyCourse(Integer cid, Integer uid) {// todo cid课程主键  uid用户主键
        FrontDeskUser userCourse = coursePcMapper.getUserCourse(uid);//查询出用户现有课程
        String purchase = userCourse.getPurchase();
        if (purchase == null || purchase.equals("")){
            Integer integer = coursePcMapper.buyCourse(uid, String.valueOf(cid));
            if (integer>=1){
                return "购买成功";
            }
            return "购买失败";
        }
        List<String> purchaseList = Arrays.asList(purchase.split(","));
        for (int i = 0; i < purchaseList.size(); i++) {
            Integer s = Integer.parseInt(purchaseList.get(i));
            if (cid == s){
                return "已经购买此课程";
            }
        }
        StringBuilder sb = new StringBuilder(purchase);
        sb.append(","+cid);
        purchase = sb.toString();
        Integer integers = coursePcMapper.buyCourse(uid, purchase);// 添加新购买的课程
        if (integers>=1){
            return "购买成功";
        }
        return "购买失败";
    }
    /*
    * @Author wuwan
    * @Description 用户信息查询
    * @Date 2023/8/24 9:54
    * @Return
    * @param null
    */
    @Override
    public CommonResult queryByUser(Integer userId) {
        return CommonResult.ok(coursePcMapper.queryByUser(userId));
    }

}
